package com.slipper.system.codeless.service;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.google.gson.reflect.TypeToken;
import com.slipper.core.cache.CacheKey;
import com.slipper.core.cache.CacheUtil;
import com.slipper.util.Tool;
import com.slipper.util.UUID;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import com.slipper.system.codeless.dao.PageCompButtonDao;
import com.slipper.system.codeless.bo.PageCompButtonBO;

import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;

@Service
@ReadTransactional
public class PageCompButtonService {
    @Resource
    private PageCompButtonDao pageCompButtonDao;
    @Resource
    private PageCompButtonConditionService pageCompButtonConditionService;
    @Resource
    private PageCompButtonUpdateService pageCompButtonUpdateService;
    
    public PageCompButtonBO queryBy(String id) {
        PageCompButtonBO pageCompButton = pageCompButtonDao.queryBy(id);
        pageCompButton.setConditionList(pageCompButtonConditionService.queryByButtonId(id));
        pageCompButton.setUpdateList(pageCompButtonUpdateService.queryByButtonId(id));
        return pageCompButton;
    }
    
    public List<PageCompButtonBO> queryList(PageCompButtonBO bo) {
        return pageCompButtonDao.queryList(bo);
    }
    
    public PageInfo<PageCompButtonBO> queryPage(PageCompButtonBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(pageCompButtonDao.queryList(bo));
    }
    
    @WriteTransactional
    public int save(String compId, List<PageCompButtonBO> list, String employeeId) {
        if (Tool.isBlank(compId)) {
            return 0;
        }

        for (int i=0;i<list.size(); i++) {
            PageCompButtonBO button = list.get(i);
            button.setCompId(compId);
            button.setOrdinal(i + 1);
            if (Tool.isBlank(button.getId())) {
                button.setId(UUID.getUUID());
                button.setUpdateId(employeeId);
                button.setCreateId(employeeId);
                button.setUpdateTime(System.currentTimeMillis());
                button.setCreateTime(System.currentTimeMillis());
                pageCompButtonDao.insert(button);
            } else {
                if (pageCompButtonDao.queryBy(button.getId()) == null) {
                    button.setUpdateId(employeeId);
                    button.setCreateId(employeeId);
                    button.setUpdateTime(System.currentTimeMillis());
                    button.setCreateTime(System.currentTimeMillis());
                    pageCompButtonDao.insert(button);
                } else {
                    button.setUpdateId(employeeId);
                    button.setUpdateTime(System.currentTimeMillis());
                    pageCompButtonDao.update(button);
                }
            }

            pageCompButtonConditionService.save(button.getId(), button.getConditionList(), employeeId);
            pageCompButtonUpdateService.save(button.getId(), button.getUpdateList(), employeeId);
        }

        // 删除被删除的数据
        PageCompButtonBO qo = new PageCompButtonBO();
        qo.setCompId(compId);
        pageCompButtonDao.queryList(qo).stream()
                .filter(button -> list.stream().noneMatch(temp -> button.getId().equals(temp.getId())))
                .forEach(this::delete);
        CacheUtil.remove(CacheKey.PageCompButton.PREFIX + compId);
        return 1;
    }

    @WriteTransactional
    public void saveList(List<PageCompButtonBO> list) {
        if (list == null) return;

        list.forEach(item -> {
            if (pageCompButtonDao.queryBy(item.getId()) != null) {
                item.setUpdateTime(System.currentTimeMillis());
                pageCompButtonDao.update(item);
            } else {
                item.setUpdateTime(System.currentTimeMillis());
                item.setCreateTime(System.currentTimeMillis());
                pageCompButtonDao.insert(item);
            }
            CacheUtil.remove(CacheKey.PageCompButton.PREFIX + item.getCompId());
        });
    }

    @WriteTransactional
    public void deleteByCompId(String compId) {
        if (Tool.isBlank(compId)) {
            return;
        }

        PageCompButtonBO qo = new PageCompButtonBO();
        qo.setCompId(compId);
        pageCompButtonDao.queryList(qo).forEach(this::delete);
        CacheUtil.remove(CacheKey.PageCompButton.PREFIX + compId);
    }

    @WriteTransactional
    public int delete(PageCompButtonBO bo) {
        pageCompButtonConditionService.deleteByButtonId(bo.getId());
        pageCompButtonUpdateService.deleteByButtonId(bo.getId());
        int result = pageCompButtonDao.delete(bo.getId());
        CacheUtil.remove(CacheKey.PageCompButton.PREFIX + bo.getCompId());
        return result;
    }

    public List<PageCompButtonBO> queryByCompId(String compId) {
        List<PageCompButtonBO> buttonList = CacheUtil.get(
                CacheKey.PageCompButton.PREFIX + compId,
                new TypeToken<List<PageCompButtonBO>>(){}.getType());
        if (buttonList == null) {
            PageCompButtonBO qo = new PageCompButtonBO();
            qo.setCompId(compId);
            buttonList = pageCompButtonDao.queryList(qo);
            buttonList.forEach(button -> {
                button.setConditionList(pageCompButtonConditionService.queryByButtonId(button.getId()));
                button.setUpdateList(pageCompButtonUpdateService.queryByButtonId(button.getId()));
            });
            CacheUtil.set(CacheKey.PageCompButton.PREFIX + compId, buttonList, 60 * 60);
        }

        return buttonList;
    }

    @WriteTransactional
    public void copyByCompId(String oldCompId, String newCompId, String employeeId) {
        queryByCompId(oldCompId).forEach(button -> {
            String oldButtonId = button.getId();
            button.setId(UUID.getUUID());
            button.setCompId(newCompId);
            button.setUpdateId(employeeId);
            button.setCreateId(employeeId);
            button.setUpdateTime(System.currentTimeMillis());
            button.setCreateTime(System.currentTimeMillis());
            pageCompButtonDao.insert(button);

            pageCompButtonConditionService.copyByButtonId(oldButtonId, button.getId(), employeeId);
            pageCompButtonUpdateService.copyByButtonId(oldButtonId, button.getId(), employeeId);
        });
    }
}
